package com.langsin.crmpro.manager;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.langsin.crmpro.dao.CompanyDao;
import com.langsin.crmpro.model.CCompany;
import static com.langsin.crmpro.utils.DTool.*;

public class CompanyManagerImpl implements CompanyManager{

	private CompanyDao companyDAO;
	
	
	public String[] addCompany(CCompany company) 
	{
		String todayStr = null;
		String id = null;
		//先判断当前用户编号在库中是否已经存在
		if(this.companyDAO.checkCode(company.getCode())){
			//不存在则直接保存
			id = this.companyDAO.addCompany(company).toString();
		}else{
			//如果存在编号+1后保存
			todayStr = (String)this.getCode().get("todayStr");
			company.setCode(todayStr);
			id = this.companyDAO.addCompany(company).toString();
		}
		return new String[]{id,todayStr};
	}
	
	public Map getCode(){
		Date d = new Date();
		String todayStr = date2Str(d, "yyyy-MM-dd");
		String num = this.companyDAO.load4AddCompany(todayStr);
		todayStr = date2Str(d, "yyyyMMdd");
		//this.code =
		
		if(Integer.parseInt(num) < 1){
			num = "1";
		}else{
			num = Integer.parseInt(num) + 1 + "";
		}
		if(num.length() == 2){
			num = "0" + num;
		}else if(num.length() == 1){
			num = "00" + num;
		}
		todayStr = "C-" + todayStr + "-" + num;
		String c_time= date2Str(d, "yyyy-MM-dd HH:mm:ss");//取得今日时间
		Map map = new HashMap();
		map.put("todayStr", todayStr);
		map.put("c_time", c_time);
		return map;
	}


	public List sDictionaryClassList() {
		// TODO Auto-generated method stub
		return this.companyDAO.sDictionaryClassList();
	}


	public List sDictionaryDetailList() {
		// TODO Auto-generated method stub
		return this.companyDAO.sDictionaryDetailList();
	}

	public List sProvinceList() {
		// TODO Auto-generated method stub
		return this.companyDAO.sProvinceList();
	}
	
	public CompanyDao getCompanyDAO() {
		return companyDAO;
	}


	public void setCompanyDAO(CompanyDao companyDAO) {
		this.companyDAO = companyDAO;
	}

}
